﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.EntityClient;
using System.Data.Linq;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using QuaManSys;

namespace QuaManSys.InnerInfo
{
    public partial class InnerInfoProcessing : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) { GetProjectNumbers(); }
        }

        protected void InfoIdlist_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowInfo(InfoIdlist.Text);
            Unenable();
            Repeater1.Visible = true;

        }



        /// <summary>
        /// 显示数据库中所有编号
        /// </summary>
        private void GetProjectNumbers()
        {
            using (var context = new QuaManSysEntities())
            {
                try
                {

                    ListItem initLi = new ListItem("--请选择--");
                    InfoIdlist.Items.Insert(0, initLi);
                    int count = 1;
                    foreach (var item in context.InnerQualityInfo)
                    {
                        ListItem li = new ListItem(item.qualityInfoId);
                        InfoIdlist.Items.Insert(count++, li);
                    }
                }
                catch (EntitySqlException ex)
                {
                    Console.WriteLine(ex.ToString());
                }

            }
        }






        /// <summary>
        ///通过qualityInfoId显示信息
        /// </summary>
        /// <param name="qualityInfoId"></param>
        protected void ShowInfo(string qualityInfoId)
        {
            using (var db = new QuaManSysEntities())
            {
                var query = db.InnerQualityInfo.Find(qualityInfoId);
                //lblNo.Text=query.qualityInfoId;
                ddlProNo.Text = query.projectNo;
                txtComponent.Text = query.components;
                txtManufac.Text = query.producer;
                txtFaiTyp.Text = query.faultType;
                if (query.projectCategory == "已处理")
                    CheckBox1.Checked = true;
                else CheckBox2.Checked = true;
                txtPjNam.Text = query.name;
                txtProDL.Text = Convert.ToString(query.deadline);
                txtCustomer.Text = query.customerLinker;
                txtCusTel.Text = query.telephone;
                txtSpeReq.Text = query.specialDemands;
                DDLInfoCla.Text = query.informationSource;
                DDLImpLev.Text = query.levelOfImportance;
                txtInfoDsc.Text = query.description;
                txtInfoWrt.Text = query.registrantName;
                txtInfoDep.Text = query.registrantDepart;
                //registDate = Convert.ToDateTime(txtWrtDat.Text),
                txtWrtDat.Text = Convert.ToString(query.registDate);
                txtReqAdv.Text = query.recommendedTreatPlan;
                lblState.Text = query.processingStatus;
                txtReqRsl.Text = query.processingResults;
                txtProCmm.Text = query.processingAssess;
                txtPjCls.Text = query.informationCategory;

            }


        }


        /// <summary>
        /// 禁止修改
        /// </summary>
           protected void Unenable()
        {
            ddlProNo.Enabled = false;
            txtComponent.ReadOnly = true;
            txtManufac.ReadOnly = true;
            txtFaiTyp.ReadOnly = true;
            CheckBox1.Enabled = false;
            CheckBox2.Enabled = false;
            txtPjNam.ReadOnly = true;
            txtProDL.ReadOnly = true;
            txtCustomer.ReadOnly = true;
            txtCusTel.ReadOnly = true;
            txtSpeReq.ReadOnly = true;
            DDLInfoCla.Enabled = false;
            DDLImpLev.Enabled = false;
            txtInfoDsc.ReadOnly = true;
            txtInfoWrt.Enabled = false;
            txtInfoDep.Enabled = false;
            //registDate = Convert.ToDateTime(txtWrtDat.Text),
            txtWrtDat.Enabled = false;
            txtReqAdv.Enabled = false;
            lblState.Enabled = false;
            txtReqRsl.Enabled = true;
            txtProCmm.Enabled = true;
            txtPjCls.Enabled = false;


        }




         /// <summary>
         /// 插入处理数据
         /// </summary>
           public void InsertProinfo(object sender, EventArgs e)
           {

               TextBox txtProName = (TextBox)Repeater1.Controls[Repeater1.Controls.Count - 1].FindControl("txtProName");
               TextBox txtProDept = (TextBox)Repeater1.Controls[Repeater1.Controls.Count - 1].FindControl("txtProDept");
               TextBox txtProOpn = (TextBox)Repeater1.Controls[Repeater1.Controls.Count - 1].FindControl("txtProOpn");
               TextBox txtProDate = (TextBox)Repeater1.Controls[Repeater1.Controls.Count - 1].FindControl("txtProDate");

                using(var db=new QuaManSysEntities())
               {
                   InnerQualityInfoPro Insertinfo = new InnerQualityInfoPro
                   {
                       processerDepart = txtProDept.Text,
                       processerName=txtProName.Text,
                       processingDate =Convert.ToDateTime(txtProDate.Text),
                       opinion = txtProOpn.Text,
                       qualityInfoId = InfoIdlist.Text

                   };

                   db.InnerQualityInfoPro.Add(Insertinfo);
                   db.SaveChanges();
  
               }
                //this.Page_Load(this, new System.EventArgs());  
                Repeater1.DataSourceID = "EntityDataSource1";
                Repeater1.DataBind();
               
           
           }

    }
}